Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post code suggestions on PRs with format issues #15477

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

timtebeek
Copy link
Contributor

These two workflows together ensure that if a pull request comes in that has formatting issues, that a github-actions bot then posts a code suggestion comment for fix any offending lines. Those comments would look something like this

image

I've used this to good effect on the OpenRewrite GitHub org, where we run some additional automations for copyright headers and coding best practices.

This has been discussed over the past year or so with @rwinch (👋🏻 ), as a good first step towards gently enforcing standards, as opposed to an unfriendly failing build pipeline with delayed feedback.

The PR leverages https://github.com/googleapis/code-suggester to turn a git diff into code suggestion comments, which we've found to work well for smaller formatting issues. https://github.com/reviewdog/reviewdog could be a possible alternative, as well as others.

I've of course not been able to test these new workflows, as it requires them to be available on the main branch first, but I've done my best to match up with the existing .github/workflows/pr-build-workflow.yml in terms of actions used. I figured to offer a first step PR here, but of course this could be generalized if desired.

No worries if you'd rather go a different direction. I figured a direct PR would be easier to review than yet another issues to discuss this once more.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 27, 2024
@timtebeek
Copy link
Contributor Author

timtebeek commented Jul 27, 2024

@markpollack did I understand correctly you'd be looking to do the same on spring-ai through ./mvnw spring-javaformat:apply and ./mvnw license:update-file-header -Plicense ? Want me to open a similar PR targeting Maven there (based on this example)?

Copy link
Contributor

@jzheaux jzheaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, @timtebeek. I've left some feedback inline. Also, since this will affect the whole team, I'll add team-attention and we can discuss at our next standup.

# Post suggestions as a comment on the PR
- uses: googleapis/code-suggester@v4
with:
command: review
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to include a comment in the patch suggestion? If so, it would be nice to inform the OP of ./gradlew format. I realize they can just accept the PR suggestion instead, I just see it as informative for the future so they remove one step from their submissions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had wondered the same internall, but unfortunately it does not appear to be an option exposed when using the code-suggester review command: https://github.com/googleapis/code-suggester?tab=readme-ov-file#review-a-pull-request
There is an alternative in the form of reviewdog that's more malleable, but also adds branding and token requirements that I'm not too pleased with, which is why I stuck with the minimal API offered by code-suggester.

@jzheaux jzheaux added in: build An issue in the build type: enhancement A general enhancement for: team-attention This ticket should be discussed as a team before proceeding and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 29, 2024
@jzheaux jzheaux self-assigned this Jul 29, 2024
@timtebeek
Copy link
Contributor Author

I like it, @timtebeek. I've left some feedback inline. Also, since this will affect the whole team, I'll add team-attention and we can discuss at our next standup.

All good; perhaps worth bringing up that this approach can be extended to cover additional cases, like missing copyright headers, or even automated code changes, like we do on our projects. For simplicity I'd started small here though, to optionally extend later.

@jzheaux
Copy link
Contributor

jzheaux commented Aug 26, 2024

@timtebeek, instead of code suggestions, could it try and push a polish commit to the PR's branch instead?

If the push fails then post a comment to the PR indicating how to fix the formatting themselves.

@timtebeek
Copy link
Contributor Author

timtebeek commented Aug 28, 2024

@timtebeek, instead of code suggestions, could it try and push a polish commit to the PR's branch instead?

If the push fails then post a comment to the PR indicating how to fix the formatting themselves.

Hmm; haven't explored that option yet, and it'd take some care to be sure that this can be done safely. Any PR may contain untrusted code, and allowing an automation to run against that code and push to a branch might be a bit of a challenge to do safely, especially if we do not want to use privileged tokens. The fun part of running an OSS project I suppose.

Let me know your thoughts on this; I don't yet know if I'll find time to explore that alternative in the near future.

The reason I'd opted for this approach of suggestions is that it opens up further automated recipe runs, and helps nudge folks to better patterns without overwriting their changes just yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: team-attention This ticket should be discussed as a team before proceeding in: build An issue in the build type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants